package com.wu.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.client.constant.RedisKeyConstant;
import com.wu.client.constant.RelationShipTypeEnum;
import com.wu.client.constant.ResTypeEnum;

import com.wu.client.dao.redis.Likes;
import com.wu.client.service.PlaylistService;
import com.wu.client.service.RelationshipService;
import com.wu.client.service.UserLikesService;
import com.wu.client.utils.ListUtil;
import com.wu.client.utils.PageRespUtil;
import com.wu.client.vo.res.PageResp;
import com.wu.client.vo.res.album.AlbumResp;
import com.wu.client.vo.res.playlist.PlaylistBaseResp;
import com.wu.client.vo.res.playlist.PlaylistResp;
import com.wu.mysql.entiy.PlaylistDO;
import com.wu.mysql.entiy.RelationshipDO;
import com.wu.mysql.mapper.PlaylistMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, PlaylistDO> implements PlaylistService {
    private final UserLikesService userLikesService;
    private final RelationshipService relationshipService;
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public PageResp<PlaylistBaseResp> getPlaylists(Integer page, Integer limit, Integer category, Long userId) {
        Page<PlaylistDO> doPage = new Page<>(page,limit);
        Page<PlaylistDO> playlistDOPage = query().eq(ObjectUtil.isNotNull(category), "category", category)
                .page(doPage);
        List<PlaylistDO> records = playlistDOPage.getRecords();
        if (CollectionUtil.isEmpty(records)){
            return new PageResp<>();
        }
        List<Long> playListIds = records.stream().map(PlaylistDO::getId).toList();
        RelationShipTypeEnum typeEnum = RelationShipTypeEnum.PLAYLIST_SONG;
        List<RelationshipDO> relationshipDOList = relationshipService.getInSourceId(typeEnum, playListIds);
        Map<Long, List<RelationshipDO>> map = relationshipDOList.stream().collect(Collectors.groupingBy(RelationshipDO::getSourceId));
        PageResp<PlaylistBaseResp> pageResp = PageRespUtil.buildPageResp(playlistDOPage, PlaylistBaseResp.class);
        List<PlaylistBaseResp> items = pageResp.getItems();
        Map<Long, Likes> longLikesMap = userLikesService.checkUserIfLike(ResTypeEnum.PLAYLIST, userId, playListIds);
        for (PlaylistBaseResp item : items) {
            List<RelationshipDO> songs = map.getOrDefault(item.getId(), new ArrayList<>());
            item.setSongCount(songs.size());
            Likes likes = longLikesMap.get(item.getId());
            if (ObjectUtil.isNotNull(likes)){
                item.setIsLiked(likes.getIsLiked());
            }
        }
        return pageResp;
    }

    @Override
    public PageResp<PlaylistBaseResp> getUserFavoritesPlaylists(Long userId, Integer page, Integer limit) {
        String userLikesRedisKey = RedisKeyConstant.getUserLikesRedisKey(ResTypeEnum.PLAYLIST.getCode(), userId);
        Set<String> playlistIds = stringRedisTemplate.opsForSet().members(userLikesRedisKey);
        if (CollectionUtil.isNotEmpty(playlistIds)){
            List<Long> ids = playlistIds.stream().map(Long::parseLong).toList();
            long count  = ids.size();
            ids = ListUtil.subListInPage(ids, page, limit);
            List<PlaylistDO> playlistDOS = this.listByIds(ids);
            if (CollectionUtil.isNotEmpty(playlistDOS)){
                List<PlaylistBaseResp> playlistBaseRespList = playlistDOS.stream().map(playlistDO -> {
                    PlaylistBaseResp playlistBaseResp = BeanUtil.toBean(playlistDO, PlaylistBaseResp.class);
                    playlistBaseResp.setArtists(playlistDO.getCreator());
                    RelationShipTypeEnum playlistSong = RelationShipTypeEnum.PLAYLIST_SONG;
                    List<RelationshipDO> relationshipDOList = relationshipService.getBySourceId(playlistSong, playlistDO.getId());
                    if (CollectionUtil.isNotEmpty(relationshipDOList)){
                        playlistBaseResp.setSongCount(relationshipDOList.size());
                    }
                    return playlistBaseResp;
                }).toList();
                return PageRespUtil.buildPageResp(page,limit,count, playlistBaseRespList);
            }
        }
        return new PageResp<>();
    }
}
